﻿/* 2011/11/25 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using Cosmos.Shortcuts;

namespace PictureManager
{
    public partial class KeyCombinationControl : UserControl
    {
        public KeyCombinationControl()
        {
            InitializeComponent();

            foreach (KeyInformation aKey in PictureManagerShortcutManager.AvailableKeys)
                keyComboBox.Items.Add(new KeyItem(aKey.Key, aKey.Description));

            foreach (ShortcutOperationInformation anOperation in PictureManagerShortcutManager.AvailableOperations)
                operationComboBox.Items.Add(new OperationItem((PictureManagerOperationCategory)anOperation.Operation, anOperation.OperationDescription));

            keyCombinationListView.ListViewItemSorter = new ShortcutActionItemSorter();
        }

        public void LoadSettings(StringCollection keyCombinationExecutants)
        {
            // Former implement.
            /*foreach (string aShortcutOperation in keyOperations)
            {
                KeyCombinationExecutant theKeyOperation = new KeyCombinationExecutant(aShortcutOperation);
                foreach (ListViewItem anItem in keyOperationListView.Items)
                {
                    KeyCombinationListViewItem theItem = (KeyCombinationListViewItem)anItem;
                    if (theItem.Key == theKeyOperation.Key)
                    {
                        theItem.Operation = theKeyOperation.Operation;
                        theItem.Parameter = theKeyOperation.Arguments[0];
                    }
                }
            }*/

            keyCombinationListView.Items.Clear();
            foreach (string anExecutant in keyCombinationExecutants)
            {
                try
                {
                    keyCombinationListView.Items.Add(new KeyCombinationListViewItem(anExecutant));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void SaveSettings(StringCollection keyCombinationExecutants)
        {
            keyCombinationExecutants.Clear();
            foreach (ListViewItem anItem in keyCombinationListView.Items)
            {
                KeyCombinationListViewItem theItem = (KeyCombinationListViewItem)anItem;
                keyCombinationExecutants.Add(theItem.EscapeString);
            }
        }

        private void keyOperationAddButton_Click(object sender, EventArgs e)
        {
            /*if (keyComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择按键", "添加失败");
                return;
            }

            if (operationComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择操作", "添加失败");
                return;
            }

            KeyItem keyItem = (KeyItem)keyComboBox.SelectedItem;
            int keyNumber = (int)(keyItem).Key;
            OperationItem operationItem = (OperationItem)operationComboBox.SelectedItem;
            int operationNumber = (int)(operationItem).Operation;
            string keyOperationText = String.Concat(keyNumber.ToString(), _KEY_OPERATION_SEPARATOR, operationNumber.ToString());
            if (operationItem.Operation == KeyOperation.OperationType.Prefix || operationItem.Operation == KeyOperation.OperationType.Postfix)
                keyOperationText = String.Concat(keyOperationText, _KEY_OPERATION_SEPARATOR, operationTextBox.Text);

            foreach (ListViewItem anItem in keyOperationListView.Items)
            {
                string keyText = keyItem.Key.ToString();
                string operationText = operationItem.Operation.ToString();

                if (keyOperationText.Equals((string)anItem.Tag))
                {
                    MessageBox.Show("已存在此设置", "添加失败");
                    return;
                }
                else if (keyItem.Key == (Keys)(anItem.SubItems[0].Tag))
                {
                    keyOperationListView.Items.Remove(anItem);
                }
            }

            ListViewItem theItem = new ListViewItem();
            ListViewItem.ListViewSubItem keySubItem = new ListViewItem.ListViewSubItem();
            keySubItem.Text = keyItem.Key.ToString();
            keySubItem.Tag = keyItem.Key;
            ListViewItem.ListViewSubItem operationSubItem = new ListViewItem.ListViewSubItem();
            operationSubItem.Text = KeyOperation.GetOperationText(operationItem.Operation);
            operationSubItem.Tag = operationItem.Operation;
            theItem.SubItems[0] = keySubItem;
            theItem.SubItems.Add(operationSubItem);
            keyOperationListView.Items.Add(theItem);*/
        }

        private void keyOperationRemoveButton_Click(object sender, EventArgs e)
        {
            /*ListView.SelectedListViewItemCollection items = keyCombinationListView.SelectedItems;
            if (items.Count > 0)
                foreach (ListViewItem anItem in items)
                    keyCombinationListView.Items.Remove(anItem);
            else
                MessageBox.Show("请选择要删除的项目", "删除失败");*/
        }

        private void keyOperationModifyButton_Click(object sender, EventArgs e)
        {

        }

        private void operationAddButton_Click(object sender, EventArgs e)
        {
            if (operationComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择操作", "添加失败");
                return;
            }

            OperationItem theOperationItem = (OperationItem)operationComboBox.SelectedItem;
            KeyCombinationListViewItem theKeyCombinationItem;
            switch (PictureManagerShortcutManager.GetParameterCount(theOperationItem.Operation))
            {
                case 0:
                    theKeyCombinationItem = new KeyCombinationListViewItem(theOperationItem.Operation);
                    break;
                case 1:
                    theKeyCombinationItem = new KeyCombinationListViewItem(theOperationItem.Operation, operationFirstArgumentTextBox.Text);
                    break;
                case 2:
                    theKeyCombinationItem = new KeyCombinationListViewItem(theOperationItem.Operation, operationFirstArgumentTextBox.Text, operationSecondArgumentTextBox.Text);
                    break;
                default: theKeyCombinationItem = null; break;
            }
            if (!PictureManagerShortcutManager.IsParameterLegal(new ShortcutOperation((int)theKeyCombinationItem.Operation, theKeyCombinationItem.Arguments)))
            {
                MessageBox.Show("参数不符合要求", "添加失败");
                return;
            }
            keyCombinationListView.Items.Add(theKeyCombinationItem);
            foreach (ListViewItem anItem in keyCombinationListView.SelectedItems)
            {
                anItem.Selected = false;
            }
            theKeyCombinationItem.Selected = true;
            theKeyCombinationItem.EnsureVisible();
        }

        private void operationRemoveButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = keyCombinationListView.SelectedItems;
            if (items.Count > 0)
                foreach (ListViewItem aItem in items)
                    keyCombinationListView.Items.Remove(aItem);
            else
                MessageBox.Show("请选择要删除的操作", "删除失败");
        }

        private void operationModifyButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = keyCombinationListView.SelectedItems;
            if (items.Count != 1)
            {
                MessageBox.Show("请选择一个要修改的操作", "修改失败");
                return;
            }

            if (operationComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择操作", "修改失败");
                return;
            }

            OperationItem theOperationItem = (OperationItem)operationComboBox.SelectedItem;
            ShortcutOperation shortcutOperation;
            switch (PictureManagerShortcutManager.GetParameterCount(theOperationItem.Operation))
            {
                case 0:
                    shortcutOperation = new ShortcutOperation((int)theOperationItem.Operation);
                    break;
                case 1:
                    shortcutOperation = new ShortcutOperation((int)theOperationItem.Operation, operationFirstArgumentTextBox.Text);
                    break;
                case 2:
                    shortcutOperation = new ShortcutOperation((int)theOperationItem.Operation, operationFirstArgumentTextBox.Text, operationSecondArgumentTextBox.Text);
                    break;
                default: shortcutOperation = new ShortcutOperation(); break;
            }
            if (!PictureManagerShortcutManager.IsParameterLegal(shortcutOperation))
            {
                MessageBox.Show("参数不符合要求", "修改失败");
                return;
            }
            ((KeyCombinationListViewItem)keyCombinationListView.SelectedItems[0]).ShortcutOperation = shortcutOperation;
        }

        private void keyCombinationAppendButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = keyCombinationListView.SelectedItems;
            if (items.Count != 1)
            {
                MessageBox.Show("请选择一个要添加组合键的操作", "添加失败");
                return;
            }

            if (keyComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择按键", "添加失败");
                return;
            }

            KeyItem theKeyItem = ((KeyItem)keyComboBox.SelectedItem);
            KeyCombination theKeyCombination = new KeyCombination(theKeyItem.Key
                , keyControlCheckBox.Checked, keyAlterCheckBox.Checked, keyShiftCheckBox.Checked);

            foreach (ListViewItem anItem in keyCombinationListView.Items)
            {
                KeyCombinationListViewItem theItem = (KeyCombinationListViewItem)anItem;
                if (theItem.Contains(theKeyCombination))
                {
                    MessageBox.Show("此组合键已经被使用", "添加失败");
                    return;
                }
            }

            ((KeyCombinationListViewItem)items[0]).Add(theKeyCombination);
        }

        private void keyCombinationClearButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = keyCombinationListView.SelectedItems;
            foreach (ListViewItem anItem in items)
                ((KeyCombinationListViewItem)anItem).Clear();
        }

        private void keyCombinationListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                KeyCombinationListViewItem theItem = (KeyCombinationListViewItem)e.Item;
                foreach (object aItem in operationComboBox.Items)
                {
                    if (((OperationItem)aItem).Operation == theItem.Operation)
                    {
                        operationComboBox.SelectedItem = aItem;
                        break;
                    }
                }
                switch (theItem.Arguments.Length)
                {
                    case 0:
                        break;
                    case 1:
                        operationFirstArgumentTextBox.Text = theItem.Arguments[0];
                        break;
                    case 2:
                        operationFirstArgumentTextBox.Text = theItem.Arguments[0];
                        operationSecondArgumentTextBox.Text = theItem.Arguments[1];
                        break;
                    default: break;
                } 
            }
        }

        private void operationComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            OperationItem theItem = (OperationItem)operationComboBox.SelectedItem;
            argumentIntroductionLabel.Text = PictureManagerShortcutManager.GetParameterDescription(theItem.Operation);
            switch (PictureManagerShortcutManager.GetParameterCount(theItem.Operation))
            {
                case 0:
                    operationFirstArgumentTextBox.Text = String.Empty; operationFirstArgumentTextBox.Enabled = false;
                    operationSecondArgumentTextBox.Text = String.Empty; operationSecondArgumentTextBox.Enabled = false;
                    break;
                case 1:
                    operationFirstArgumentTextBox.Enabled = true;
                    operationSecondArgumentTextBox.Text = String.Empty; operationSecondArgumentTextBox.Enabled = false;
                    break;
                case 2:
                    operationFirstArgumentTextBox.Enabled = true;
                    operationSecondArgumentTextBox.Enabled = true;
                    break;
                default: break;
            }
        }

        private void keyComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            ComboBox self = sender as ComboBox;
            foreach (object aItem in self.Items)
            {
                if (e.KeyCode == ((KeyItem)aItem).Key)
                {
                    self.DroppedDown = false;
                    self.SelectedItem = aItem;
                    break;
                }
            }
        }
    }
}
